package com.erp.warehouse.clouderp_warehouse.controller;

import com.erp.warehouse.clouderp_warehouse.pojo.outbound.OutboundOrderCreate;
import com.erp.warehouse.clouderp_warehouse.pojo.outbound.OutboundOrderModify;
import com.erp.warehouse.clouderp_warehouse.service.outbound.OutboundService;
import com.erp.warehouse.clouderp_warehouse.util.UniqueOrderCode;
import org.comple.erp.pojo.ResultStatus;
import org.comple.erp.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 2025/11/6 16:24
 * author: 向远洋
 */
@RestController
@RequestMapping("/outbound")
@RefreshScope
public class OutboundOrderController {
    @Autowired
    OutboundService outboundService;
    @Autowired
    UniqueOrderCode uniqueOrderCode;
    @Value("${auth.staffNo}")
    String staffNo;
    @Value("${auth.companyCode}")
    String companyCode;
    @PostMapping("/page")
    public AjaxResult getPage(@RequestBody Map<String, Object> params) {
        //根据jwt获取用户，和公司
        params.put("staffNo", staffNo);
        params.put("companyCode", companyCode);
        return AjaxResult.back(ResultStatus.SUCCESS, "获取出库表分页数据成功！")
                .add("pageInfo", outboundService.getPageInfo(params));
    }

    @PostMapping("/get/{id}")
    public AjaxResult getOutboundOrderWithItems(@PathVariable Long id) {
        return AjaxResult.back(ResultStatus.SUCCESS, "获取出库单及明细信息成功！")
                .add("outboundOrderRequest", outboundService.getOutboundOrderCreate(id));
    }

    @PostMapping("/create")
    public AjaxResult createOutboundOrderWithItems(@RequestBody OutboundOrderCreate request) throws InterruptedException {
        //从jwt获取到用户，和相关公司code
        //获取到当前订单编号
        String code = "CK" + uniqueOrderCode.getCode(companyCode, "OUTBOUND");
        request.getOutboundOrder().setCode(code);
        request.getOutboundOrder().setCompanyCode(companyCode);
        request.getOutboundOrder().setStaffNo(staffNo);
        outboundService.createWithItems(request);
        return AjaxResult.back(ResultStatus.SUCCESS, "成功！");
    }

    @PostMapping("/modify")
    public AjaxResult modifyOutboundOrderWithItems(@RequestBody OutboundOrderModify outboundOrderModify) {
        outboundService.modifyWithItems(outboundOrderModify);
        return AjaxResult.back(ResultStatus.SUCCESS, "出库单和明细修改成功！");
    }

}
